//
//  File.swift
//  Gamers
//
//  Created by 虚空之翼 on 15/8/12.
//  Copyright (c) 2015年 Freedom. All rights reserved.
//

import Foundation
import RealmSwift
import SwiftyJSON

class Live: Object {
    // 用户
    dynamic var userId = ""
    dynamic var userName = ""
    dynamic var firstName = ""
    dynamic var lastName = ""
    dynamic var email = ""
    dynamic var communityId = 0
    dynamic var avatar = ""
    dynamic var streamBanner = ""
    dynamic var about = ""
    
    dynamic var type = ""           //直播类型
    
    // 视频数据流
    dynamic var streamId = ""
    dynamic var streamUrl = ""
    dynamic var date = ""
    dynamic var steamDescription = ""
    dynamic var title = ""
    dynamic var views = 0
    dynamic var chatUrl = ""
    dynamic var gameName = ""
    dynamic var channelViews = 0
    dynamic var channelFollowers = 0
    dynamic var streamAbout = ""
    
    dynamic var thumbnailMedium = ""
    dynamic var thumbnailLarge = ""
    
    class func collection(json: JSON) -> [Live] {
        var collection = [Live]()
        
        if let items = json.array {
            for item in items {
                let gameItem = Live.modelFromJSON(item)
                collection.append(gameItem)
            }
        }
        return collection
    }
    
    // 把JSON数据转换为对象
    class func modelFromJSON(json: JSON) -> Live {
        let model = Live()
        
        if let type = json["type"].string { model.type = type }

        if let userId = json["user"]["user_id"].string { model.userId = userId }
        if let userName = json["user"]["username"].string { model.userName = userName }
        if let firstName = json["user"]["first_name"].string { model.firstName = firstName }
        if let lastName = json["user"]["last_name"].string { model.lastName = lastName }
        if let email = json["user"]["email"].string { model.email = email }
        if let communityId = json["user"]["community_id"].int { model.communityId = communityId }
        if let avatar = json["user"]["avatar"].string { model.avatar = avatar }
        if let streamBanner = json["user"]["stream_banner"].string { model.streamBanner = streamBanner }
        if let about = json["user"]["about"].string { model.about = about }
        
        if let streamId = json["stream"]["id"].string { model.streamId = streamId }
        if let streamUrl = json["stream"]["stream_url"].string { model.streamUrl = streamUrl }
        if let date = json["stream"]["date"].string { model.date = date }
        if let steamDescription = json["stream"]["description"].string { model.steamDescription = steamDescription }
        if let title = json["stream"]["title"].string { model.title = title }
        if let views = json["stream"]["views"].int { model.views = views }
        if let chatUrl = json["stream"]["chat_url"].string { model.chatUrl = chatUrl }
        if let gameName = json["stream"]["game_name"].string { model.gameName = gameName }
        if let channelViews = json["stream"]["channel_views"].int { model.channelViews = channelViews }
        if let channelFollowers = json["stream"]["channel_followers"].int { model.channelFollowers = channelFollowers }
        if let streamAbout = json["stream"]["about"].string { model.streamAbout = streamAbout }
        
        if let thumbnailMedium = json["stream"]["thumbnail"]["medium"].string { model.thumbnailMedium = thumbnailMedium }
        if let thumbnailLarge = json["stream"]["thumbnail"]["large"].string { model.thumbnailLarge = thumbnailLarge }
        
        return model
    }
    
}
